﻿using Finance.Infrastructrue.Interface;
using Finance.API.Write.Application.Command.FinancialCategoryCommand;
using Finance.Domain;
using Finance.ErrorCode;
using MediatR;
using AutoMapper;
using System.DirectoryServices.Protocols;
using Finance.Infrastructrue.Implement;

namespace Finance.API.Write.Application.Hander.FinancialCategoryHander
{
    public class UpdFinancialCategoryHander:IRequestHandler<UpdFinancialCategoryCommand,APIResult<int>>
    {
      

        DatabaseContext Db;
        IBaseRepository<FinancialCategory> finRep;
        ILogger<UpdFinancialCategoryHander>  logger;
        IMapper mapper;

        public UpdFinancialCategoryHander(DatabaseContext db, IBaseRepository<FinancialCategory> finRep, ILogger<UpdFinancialCategoryHander> logger, IMapper mapper)
        {
            Db = db;
            this.finRep = finRep;
            this.logger = logger;
            this.mapper = mapper;
        }

        public  async Task<APIResult<int>> Handle(UpdFinancialCategoryCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                var financial = mapper.Map<FinancialCategory>(request);
                financial.IsDel = false;
                var m = await Db.SqlSugar.Updateable(financial).ExecuteCommandAsync();

                if (m > 0)
                {
                    result.Code = APIEnum.Ok;
                    result.Message = "财务数据财务类别修改成功";
                    result.Data = m;
                }
                else
                {
                    result.Code = APIEnum.Fail;
                    result.Message = "财务数据财务类别修改失败" ;
                }
               
               
            }
            catch (Exception ex)
            {
               logger.LogError( "财务数据财务类别修改报错" + ex.Message);
                throw;
                
            }
            return await Task.FromResult(result);
        }

    }
}
